<template>
  <layout>
    <!-- 顶部操作 -->
    <section slot="search">
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        :FormList="searchColumns"
      >
        <a-button
          class="action-button"
          type="primary"
          @click="search()"
        >搜索</a-button>
        <a-button
          class="action-button"
          type="primary"
          auth="power-group.add"
          @click="tableAdd"
        >新增</a-button>
      </custom-form>
      <!-- <a-input placeholder="产品名称或者编号" v-model="tableSearch.searchKey" allowClear style="width: 200px;" /> -->
    </section>

    <!-- 产品列表 -->
    <a-table
      :columns="tableColumns"
      :dataSource="tableData"
      :rowKey="record => record.pow_gp_id"
      :customRow="customTableRow"
      :pagination="false"
      bordered
    >
      <template
        slot="pow_gp_nam"
        slot-scope="row"
      >
        {{getLabel(row.pow_gp_nam, [], 'pow_gp_nam')}}
      </template>

      <!-- <template
        slot="power_ids"
        slot-scope="row"
      >
        {{getLabel(row.power_ids, powerIds, 'power_ids')}}
      </template> -->
      
      <!-- 按钮区 -->
      <template
        slot="action"
        slot-scope="row"
      >
        <a-button
          class="action-button"
          type="link"
          auth="power-group.edit"
          @click="handleTableEdit(row)"
        >编辑</a-button>
        <a-popconfirm
          title="您确认要删除该条数据?"
          ok-text="确认"
          cancel-text="取消"
          @confirm="tableDel(row)"
        >
          <a-button
            class="action-button"
            auth="power-group.delete"
            type="link"
          >删除</a-button>
        </a-popconfirm>
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination>

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      :footer="null"
      @cancel="visible = false"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="formMode"
        layout="horizontal"
      ></custom-form>
      <!-- <section slot="footer"> -->
      <a-button
        @click="handleSubmit"
        type="primary"
      >提交</a-button>
      <!-- </section> -->
    </a-modal>

  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue } from 'vue-property-decorator'
import { State, Mutation, Getter, namespace } from 'vuex-class'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { poserServices, IPower, IPowerGroup } from '@/common/services/power'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm
  }
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  // 产品列表搜索条件
  public tableSearch = {
    pow_gp_nam: ''
  }

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '权限组编号',
      dataIndex: 'pow_gp_id',
      key: 'pow_gp_id',
      width: 100,
      form: {
        type: 'a-input',
        placeholder: '编号自动生成',
        disabled: true
      }
    },
    {
      title: '权限组名称',
      key: 'pow_gp_nam',
      scopedSlots: { customRender: 'pow_gp_nam' },
      width: 200,
      form: {
        type: 'a-input',
        placeholder: '请输入权限组名称',
        isShowQuery: true
      }
    },
    {
      title: '权限列表',
      dataIndex: 'power_ids',
      key: 'power_ids',
      form: {
        type: 'a-tree',
        placeholder: '请输入工作单位',
        replaceFields: {
          title: 'power_nam',
          key: 'power_id'
        },
        checkStrictly: true,
        stringToArray: true,
        checkable: true,
        treeData: []
      }
    },
    {
      title: '操作',
      key: 'action',
      width: 120,
      scopedSlots: { customRender: 'action' }
    }
  ]

  public tableApi = {
    list: poserServices.getPowerGroupList,
    add: poserServices.updatePowerGroup,
    edit: poserServices.updatePowerGroup,
    del: poserServices.deletePowerGroup,
    imageUpload: 'uploadStoreImage',
    imageRemove: 'delStoreImage'
  }

  // public powerIds: any[] = []

  public mounted() {
    this.getPowerList()
    this.getData()
  }

  public async getData() {
    const res = await this.getTableList()
    this.tableData = res.data || []
  }

  public getPowerList() {
    poserServices.getPowerList().then(res => {
      this.columns.forEach(item => {
        if (item.key === 'power_ids') {
          item.form && (item.form.treeData = res.data || [])
          // this.powerIds = res.data.map(item => {
          //   return {
          //     label: item.power_nam,
          //     value: item.power_id
          //   }
          // }) || []
        }
      })
    })
  }

  /**
   * 调用编辑接口提交数据
   *
   * @param {object} [values={}]
   * @memberof TableMixin
   */
  public editApi(values: any, options = {}) {
    let params = Object.assign(
      {},
      {
        ...this.currentRow,
        ...this.commonParams,
        ...values
      }
    )
    this.visibleLoading = true
    // 编辑店铺信息的时候，未审核通过的不能修改上下线状态，不传此参数
    if (+values.zt === 3 || +values.zt === 5) {
      delete params.zt
    }
    this.tableApi
      .edit(params)
      .then((res: Api.ResponseOptions) => {
        this.visibleLoading = false
        this.visible = false
        setTimeout(() => {
          this.$message.success('编辑成功')
          this.getData()
        }, 200)
      })
      .catch(() => {
        this.visibleLoading = false
      })
  }

  // 编辑前数据预处理
  public async handleTableEdit(row: any) {
    this.tableEdit(Object.assign({}, row))
  }
}
</script>

<style scoped>
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
</style>
